package com.lanou.utp.service.impl;

import com.lanou.utp.entity.Teacher;
import com.lanou.utp.entity.User;
import com.lanou.utp.mapper.BaseMapper;
import com.lanou.utp.mapper.RoleMapper;
import com.lanou.utp.mapper.TeacherMapper;
import com.lanou.utp.mapper.UserMapper;
import com.lanou.utp.service.TeacherEmpService;
import com.lanou.utp.entity.ResponseData;
import org.springframework.context.annotation.Role;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("teacherEmpService")
public class TeacherEmpServiceImpl implements TeacherEmpService {

    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public Map selectTeacherByPage(Integer page) {
        int size = 10*(page-1);
        List<Teacher> teachers = teacherMapper.selectTeacherByPage(size);
        List<Teacher> data = new ArrayList<>();
        for (Teacher teacher : teachers) {
            Long id = teacher.getId();
            User user = userMapper.selectUserById(id);
            teacher.setUser(user);
            data.add(teacher);
        }
        Integer total = teacherMapper.selectTotal();
        HashMap<String,Object> paging = new HashMap<>();
        paging.put("total",total);
        paging.put("size",10);
        paging.put("page",page);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message","ok");
        map.put("data",data);
        map.put("paging",paging);
        return map;
    }

    @Override
    @Transactional
    public ResponseData addTeacher(Teacher teacher) {
        User teacherUser = teacher.getUser();
        userMapper.addTeacher(teacherUser);
        Long userId =  teacherUser.getId();
//        System.out.println("=======userId======="+userId);
        teacher.setId(userId);
        teacherMapper.addTeacher(teacher);
        Long baseId = teacher.getBaseId();
        if (baseId == null){
            //校内教师
            roleMapper.addBaseRole(userId);
        }else {
            //校外基地教师
            roleMapper.addTeacherRole(userId);
        }

       return new ResponseData("ok");
    }

    @Override
    @Transactional
    public  ResponseData deleteOneTeacher(Integer id) {
        teacherMapper.delTeacher(id);
        userMapper.delUser(id);

        return new ResponseData("ok");
    }

    @Override
    @Transactional
    public ResponseData deleteSomeTeachers(List<Integer> ids) {
//        for (Integer id : ids) {
//            teacherMapper.delTeacher(id);
//            userMapper.delTeacher(id);
//        }
        teacherMapper.delTeachers(ids);
        userMapper.delUsers(ids);

        return new ResponseData("ok");
    }

    @Override
    @Transactional
    public ResponseData updataTeacher(Teacher teacher) {
        User user = teacher.getUser();
        Long id = teacher.getId();
        user.setId(id);
        userMapper.updateTeacher(user);
        teacherMapper.updateTeacher(teacher);
        Long baseId = teacher.getBaseId();
        int role;
        if (baseId == null){
            role = 1;
            roleMapper.updateRole(role,id);
        }else {
            role = 3;
            roleMapper.updateRole(role,id);
        }
        return new ResponseData("ok");
    }
}
